package com.winning.pmph.utils;

import com.winning.pmph.entity.KnowledgeLibField;
import com.winning.pmph.entity.Rule;
import com.winning.pmph.entity.Tag;
import com.winning.pmph.service.RuleService;
import com.winning.pmph.service.TagService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.util.List;
import java.util.Objects;

public class KnowledgeLibFiledUtils {
    static int flag = 1;

    private static TagService tagService = ApplicationContextUtil.getBean(TagService.class);
    private static RuleService ruleService = ApplicationContextUtil.getBean(RuleService.class);

    public static HSSFWorkbook createExcel(List<KnowledgeLibField> list, String name) {

        // 创建一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook(); // 创建一个工作表
        HSSFSheet sheet = wb.createSheet(name + "字段导出");

        // 添加表头行
        HSSFRow hssfRow = sheet.createRow(0);

        // 添加表头内容
        HSSFCell headCell = hssfRow.createCell(0);
        headCell.setCellValue("知识库字段名称");

        headCell = hssfRow.createCell(1);
        headCell.setCellValue("字段类型");

        headCell = hssfRow.createCell(2);
        headCell.setCellValue("关联分类");

        headCell = hssfRow.createCell(3);
        headCell.setCellValue("选项");

        headCell = hssfRow.createCell(4);
        headCell.setCellValue("是否必填");

        // 添加数据内容
        for (int i = 0; i < list.size(); i++) {
            hssfRow = sheet.createRow(i + 1);
            // OperationLog operationLog = list.get(i);
            hssfRow.setHeightInPoints(20);

            KnowledgeLibField kild = list.get(i);
            // 创建单元格，并设置值
            HSSFCell cell = hssfRow.createCell(0);
            cell.setCellValue(kild.getName());

            cell = hssfRow.createCell(1);
            int kind = kild.getKind();
            if (kind == KnowledgeLibField.FIELD_KIND_STRINGTEXT) {
                cell.setCellValue("输入框");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_RICHEDITOR) {
                cell.setCellValue("富文本");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_REFERENCE) {
                cell.setCellValue("参照");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_DROPDOWNLIST) {
                cell.setCellValue("单选下拉框");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_IMG) {
                cell.setCellValue("图片");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_DROPMULTILIST) {
                cell.setCellValue("多选下拉框");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_TEXTAREA) {
                cell.setCellValue("文本域");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_MULTI_VALUE_INPUT) {
                cell.setCellValue("多值输入框");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_ASSOCIATE_CURRENT_KNOWLEDGE_LIB) {
                cell.setCellValue("关联本知识库");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_ASSOCIATE_OTHER_KNOWLEDGE_LIB) {
                cell.setCellValue("关联其他知识库");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_ASSOCIATE_PLATFORM) {
                cell.setCellValue("关联应用平台");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_VIDEO) {
                cell.setCellValue("视频");
            }
            if (kind == KnowledgeLibField.FIELD_KIND_FILE) {
                cell.setCellValue("文件");
            }

            cell = hssfRow.createCell(2);
            String tagId = kild.getKindValue();
            if (StringUtils.isNotBlank(tagId)) {
                Tag tag = tagService.getById(tagId);
                if (tag != null) {
                    cell.setCellValue(tag.getName());
                }
            }

            cell = hssfRow.createCell(3);
            String kindValue = kild.getKindValue();
            if (StringUtils.isNotBlank(kindValue)) {
                String sql1 = "select * from rule where id =" + "'" + kindValue + "'";
                Rule rule = ruleService.getById(kindValue);
                if (Objects.nonNull(rule)) {
                    cell.setCellValue(rule.getName());
                } else {
                    cell.setCellValue("");
                }
            }


            cell = hssfRow.createCell(4);
            if (!kild.getRequired()) {
                cell.setCellValue("不必填");
            } else {
                cell.setCellValue("必填");
            }

        }
        return wb;
    }

    public static int deleteFile(File file) {
        if (!file.exists()) {

            System.out.println("文件删除失败,请检查文件路径是否正确");
            return 0;
        }
        // 取得这个目录下的所有子文件对象
        File[] files = file.listFiles();
        // 遍历该目录下的文件对象
        for (File f : files) {
            // 打印文件名
            String name = file.getName();
            // 判断子目录是否存在子目录,如果是文件则删除
            if (f.isDirectory()) {
                deleteFile(f);
                flag = 1;
            } else {
                f.delete();
            }

        }
        // 删除空文件夹 for循环已经把上一层节点的目录清空。
        return flag;
    }


}
